/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onlylemi.mapview.slice;

import com.onlylemi.mapview.ToastUtil;
import com.onlylemi.mapview.library.MapView;
import com.onlylemi.mapview.library.MapViewListener;
import com.onlylemi.mapview.ResourceTable;
import com.onlylemi.mapview.library.layer.BitmapLayer;
import com.onlylemi.mapview.library.utils.MapUtils;
import com.onlylemi.mapview.menu.MenuDialog;
import com.onlylemi.mapview.menu.MenuProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Point;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class BitmapLayerTestSlice extends AbilitySlice implements Component.ClickedListener {

    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");

    private MapView mapView;
    private BitmapLayer bitmapLayer;

    private List<String> itemList;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_bitmap_layer_test);

        Text tvTitle = (Text) findComponentById(ResourceTable.Id_tv_title);
        tvTitle.setText("BitmapLayerTest");
        Image ivMenu = (Image) findComponentById(ResourceTable.Id_iv_more);
        ivMenu.setVisibility(Component.VISIBLE);
        ivMenu.setClickedListener(this);

        itemList = new ArrayList<>();
        itemList.add("Change Bitmap Positon");
        itemList.add("Set Bitmap Scale");

        mapView = (MapView) findComponentById(ResourceTable.Id_mapview);
        PixelMap bitmap = MapUtils.getPixelMap(this, ResourceTable.Media_map);
        mapView.loadMap(bitmap);
        mapView.setMapViewListener(new MapViewListener() {
            @Override
            public void onMapLoadSuccess() {
                HiLog.info(LABEL, "onMapLoadSuccess");

                PixelMap bmp = MapUtils.getPixelMap(BitmapLayerTestSlice.this, ResourceTable.Media_icon);
                bitmapLayer = new BitmapLayer(mapView, bmp);
                bitmapLayer.setLocation(new Point(400, 400));
                bitmapLayer.setOnBitmapClickListener(new BitmapLayer.OnBitmapClickListener() {
                    @Override
                    public void onBitmapClick(BitmapLayer layer) {
                        ToastUtil.show("click");
                    }
                });
                mapView.addLayer(bitmapLayer);
                mapView.refresh();
            }

            @Override
            public void onMapLoadFail() {
                HiLog.info(LABEL, "onMapLoadFail");
            }
        });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_iv_more:
                MenuDialog.with(this)
                        .load(itemList)
                        .setListener(new MenuDialog.OnItemClickListener() {
                            @Override
                            public void onItemClick(MenuProvider adapter, int position) {
                                switch (position) {
                                    case 0:
                                        // change bmp postion
                                        int x = new SecureRandom().nextInt((int) mapView.getMapWidth());
                                        int y = new SecureRandom().nextInt((int) mapView.getMapHeight());
                                        bitmapLayer.setLocation(new Point(x, y));
                                        mapView.refresh();
                                        break;
                                    case 1:
                                        // change bmp is/not scale
                                        bitmapLayer.setAutoScale(!bitmapLayer.isAutoScale());
                                        if (bitmapLayer.isAutoScale()) {
                                            itemList.set(position, "Set Bitmap Not Scale");
                                        } else {
                                            itemList.set(position, "Set Bitmap Scale");
                                        }
                                        adapter.notifyDataSetItemChanged(position);
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }).showDialog();
                break;
            default:
                break;
        }
    }
}
